//
//  VideoBottomBar.m
//  PhotoBrowser
//
//  Created by zzg on 2021/8/30.
//

#import "MSVideoBottomBar.h"
#import <Masonry/Masonry.h>

@interface MSVideoBottomBar()

@property(nonatomic, strong)UIView * backgroudView;         // 黑色透明背景


@end




@implementation MSVideoBottomBar
+ (instancetype)videoTMSar {
    return [[MSVideoBottomBar alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configUIView];
    }
    return self;
}

- (void)configUIView {
    __weak typeof(self) weakSelf = self;
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.backgroudView];
    [self.backgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    [self addSubview:self.playMutedBtn];
    [self.playMutedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-12);
        make.width.height.mas_equalTo(30);
    }];
    
    
    [self addSubview:self.playPauseBtn];
    [self.playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.playMutedBtn.mas_right).offset(17);
        make.centerY.mas_equalTo(self.playMutedBtn);
        make.width.height.mas_equalTo(30);
    }];
    
    
    [self addSubview:self.currentTimeLabel];
    [self.currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.playPauseBtn.mas_right).mas_offset(5);
//        make.top.mas_equalTo(weakSelf.playPauseBtn.mas_top);
        make.centerY.mas_equalTo(self.playPauseBtn);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    
    [self addSubview:self.totalTimeLabel];
    [self.totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf).mas_offset(-10);
//        make.top.mas_equalTo(weakSelf.currentTimeLabel.mas_top);
        make.centerY.mas_equalTo(self.currentTimeLabel);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    
    [self addSubview:self.playingProgressSlider];
    [self.playingProgressSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
//        make.centerY.mas_equalTo(weakSelf.totalTimeLabel);
        make.centerY.mas_equalTo(self.totalTimeLabel);
        make.left.equalTo(weakSelf.currentTimeLabel.mas_right).mas_offset(2);
        make.right.equalTo(weakSelf.totalTimeLabel.mas_left).mas_offset(-2);
    }];
    
    
//    [self.playPauseBtn set_hitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
    
}

- (UIView *)backgroudView
{
    if (!_backgroudView) {
        _backgroudView = [[UIView alloc]init];
//        if (k_IS_IPHONEX_LATER) {
//            // 因为iphoneX系统已经自带黑边  所以就不透明
//            _backgroudView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
//        }else {
            _backgroudView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.65];
//        }
    }
    return _backgroudView;
}

- (UIButton *)playMutedBtn {
    if (!_playMutedBtn) {
        _playMutedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _playMutedBtn.showsTouchWhenHighlighted = YES;
//        BOOL muted = [[NSUserDefaults standardUserDefaults] ms_boolForKey:@"ldMutedItemValue"];
//        NSString * imageName = muted ? @"video_player_muted" : @"video_player_not_muted";
//        _playMutedBtn.selected = muted;
//        [_playMutedBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [_playMutedBtn addTarget:self action:@selector(playMutedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playMutedBtn;
}

- (UIButton *)playPauseBtn {
    if (!_playPauseBtn) {
        _playPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _playPauseBtn.showsTouchWhenHighlighted = YES;
        [_playPauseBtn setImage:[UIImage imageNamed:@"video_bottom_pause"] forState:UIControlStateNormal];
        [_playPauseBtn addTarget:self action:@selector(playPauseBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playPauseBtn;
}

- (UILabel *)currentTimeLabel {
    if (!_currentTimeLabel) {
        _currentTimeLabel = [[UILabel alloc] init];
        _currentTimeLabel.textColor = [UIColor whiteColor];
        _currentTimeLabel.textAlignment = NSTextAlignmentLeft;
        _currentTimeLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _currentTimeLabel;
}

- (UILabel *)totalTimeLabel {
    if (!_totalTimeLabel) {
        _totalTimeLabel = [[UILabel alloc]init];
        _totalTimeLabel.textColor = [UIColor whiteColor];
        _totalTimeLabel.textAlignment = NSTextAlignmentCenter;
        _totalTimeLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _totalTimeLabel;
}

- (UISlider *)playingProgressSlider {
    if (!_playingProgressSlider) {
        _playingProgressSlider = [[UISlider alloc] init];
        _playingProgressSlider.minimumTrackTintColor = [UIColor whiteColor];
        _playingProgressSlider.maximumTrackTintColor = [UIColor colorWithRed:172/255.0 green:172/255.0 blue:172/255.0 alpha:1];
        [_playingProgressSlider setThumbImage:[UIImage imageNamed:@"video_slider_thumb"] forState:UIControlStateNormal];
        [_playingProgressSlider addTarget:self action:@selector(sliderChanging:) forControlEvents:UIControlEventValueChanged];
        [_playingProgressSlider addTarget:self action:@selector(sliderDidEndChange:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
    }
    return _playingProgressSlider;
}

- (UIProgressView *)cacheProgressView {
    if (!_cacheProgressView) {
        _cacheProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        _cacheProgressView.progressTintColor = [UIColor colorWithWhite:1 alpha:0.8];
        _cacheProgressView.progressTintColor = [UIColor redColor];
        _cacheProgressView.trackTintColor = [UIColor clearColor];
        _cacheProgressView.layer.cornerRadius = 0.5;
        _cacheProgressView.layer.masksToBounds = YES;
    }
    return _cacheProgressView;
}

- (void)playMutedBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSString * imageName = sender.selected ? @"video_player_muted" : @"video_player_not_muted";
    [_playMutedBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(videoBottomBarDidClickMuted:)]) {
        [self.delegate videoBottomBarDidClickMuted:sender.selected];
    }
    
    BOOL isMuted = sender.selected;
//    [[NSUserDefaults standardUserDefaults] setSafeObject:@(isMuted) forKey:@"ldMutedItemValue"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)playPauseBtnAction {
    if ([_delegate respondsToSelector:@selector(videoBottomBarDidClickPlayPauseBtn)]) {
        [_delegate videoBottomBarDidClickPlayPauseBtn];
    }
}

- (void)sliderChanging:(UISlider *)sender {
    if ([_delegate respondsToSelector:@selector(videoBottomBarChangingSlider:)]) {
        [_delegate videoBottomBarChangingSlider:sender];
    }
    
    self.currentTimeLabel.text = [self formatTimeWith:(long)floor(sender.value)];
}

- (void)sliderDidEndChange:(UISlider *)sender {
    if ([_delegate respondsToSelector:@selector(videoBottomBarDidEndChangeSlider:)]) {
        [_delegate videoBottomBarDidEndChangeSlider:sender];
    }
    self.currentTimeLabel.text = [self formatTimeWith:(long)floor(sender.value)];
}

// 重置所有控件状态
- (void)resetTheBottomView {
    self.totalTimeLabel.text = [self formatTimeWith:0];
    self.currentTimeLabel.text = [self formatTimeWith:0];
    self.playingProgressSlider.minimumValue = 0;
    self.playingProgressSlider.maximumValue = 0;
    [self.cacheProgressView setProgress:0];
    [self.playPauseBtn setImage:[UIImage imageNamed:@"video_bottom_play"] forState:UIControlStateNormal];
}

// 根据视频长度设置控件状态,如视频总时长，进度条最大值
- (void)showStartWithVideoDuration:(CGFloat)videoDuration {
    NSInteger videoDurationTime = round(videoDuration);
    if (videoDurationTime == 0) {
        videoDuration = 1;
    }
    self.totalTimeLabel.text = [self formatTimeWith:videoDurationTime];
    self.currentTimeLabel.text = [self formatTimeWith:0];
    self.playingProgressSlider.minimumValue = 0;
    self.playingProgressSlider.maximumValue = videoDuration;
}

// 根据是否播放状态控制按钮左侧按钮显示
- (void)divideThePlayPauseButtonWithPlayStatus:(BOOL)playStatus {
    if (playStatus) {
        // 播放状态设置暂停照片
        [self.playPauseBtn setImage:[UIImage imageNamed:@"video_bottom_pause"] forState:UIControlStateNormal];
    }else {
        // 暂停状态设置播放照片
        [self.playPauseBtn setImage:[UIImage imageNamed:@"video_bottom_play"] forState:UIControlStateNormal];
    }
}

// 根据当前播放进度设置UI
- (void)showProgressWithCurrentVideoTime:(CGFloat)currentTime
{
    if (currentTime < 0) {
        currentTime = 0;
    }
    self.currentTimeLabel.text = [self formatTimeWith:(long)floor(currentTime)];
    [self.playingProgressSlider setValue:currentTime];
}

- (NSString *)formatTimeWith:(long)time {
    NSString *formatTime = nil;
    if (time < 3600) {
        formatTime = [NSString stringWithFormat:@"%02li:%02li", lround(floor(time / 60.0)), lround(floor(time / 1.0)) % 60];
    } else {
        formatTime = [NSString stringWithFormat:@"%02li:%02li:%02li", lround(floor(time / 3600.0)), lround(floor(time % 3600) / 60.0), lround(floor(time / 1.0)) % 60];
    }
    return formatTime;
}





@end





